# coding=utf-8
"""
https://github.com/MZCretin/RollToolsApi#获取特定城市今日天气
获取特定城市今日天气
"""
import requests

__all__ = ['get_rttodayweather', 'WEEK_DICT']

WEEK_DICT = {"1": '星期一', "2": '星期二', "3": '星期三', "4": '星期四', "5": '星期五', "6": '星期六', "7": '星期日'}


# {"code":1,"msg":"数据返回成功","data":{"address":"广西壮族自治区 桂林市 全州县",
# "cityCode":"450324","temp":"26℃","weather":"晴","windDirection":"东北","windPower":"≤3级",
# "humidity":"58%","reportTime":"2019-06-14 10:49:37"}}

def get_rttodayweather(cityname):
    """
    获取特定城市今日天气
     https://github.com/MZCretin/RollToolsApi#获取特定城市今日天气
    :param cityname:str 传入你需要查询的城市，请尽量传入完整值，否则系统会自行匹配，可能会有误差
    :return:str 天气(2019-06-12 星期三 晴 南风 3-4级 高温 22.0℃ 低温 18.0℃ 愿你拥有比阳光明媚的心情)
    """
    print('获取 {} 的天气...'.format(cityname))
    app_id = 'piijvitmpjbjhvmn'
    app_secret = 'aU5wZ29oMXNzQkc5K2RUWXZybzBYZz09'

    if not app_id or not app_secret:
        print('app_id 或 app_key 为空，请求失败')
        return

    try:
        # forecast
        resp = requests.get(
            'https://www.mxnzp.com/api/weather/forecast/{}?app_id={}&app_secret={}'.format(cityname, app_id,
                                                                                           app_secret))
        print(resp.text)
        if resp.status_code == 200:
            content_dict = resp.json()
            if content_dict['code'] == 1:
                data_dict = content_dict['data']
                forecasts = data_dict['forecasts'][0]
                forecasts1 = data_dict['forecasts'][1]
                forecasts2 = data_dict['forecasts'][2]
                # report_time = report_time.split(' ')[0]
                return_text = '{address} \n' \
                              '{date} {dayOfWeek} \n' \
                              '天气{dayWeather} 气温{dayTemp} 风向{dayWindDirection} 风力{dayWindPower}\n' \
                              '{date1} {dayOfWeek1} \n' \
                              '天气{dayWeather1} 气温{dayTemp1} 风向{dayWindDirection1} 风力{dayWindPower1}\n' \
                              '{date2} {dayOfWeek1} \n' \
                              '天气{dayWeather2} 气温{dayTemp2} 风向{dayWindDirection2} 风力{dayWindPower2}\n'.format(
                    address=data_dict['address'],
                    dayOfWeek=WEEK_DICT[forecasts['dayOfWeek']],
                    date=forecasts['date'],
                    dayWeather=forecasts['dayWeather'],
                    dayTemp=forecasts['dayTemp'],
                    dayWindDirection=forecasts['dayWindDirection'],
                    dayWindPower=forecasts['dayWindPower'],

                    address1=data_dict['address'],
                    dayOfWeek1=WEEK_DICT[forecasts1['dayOfWeek']],
                    date1=forecasts1['date'],
                    dayWeather1=forecasts1['dayWeather'],
                    dayTemp1=forecasts1['dayTemp'],
                    dayWindDirection1=forecasts1['dayWindDirection'],
                    dayWindPower1=forecasts1['dayWindPower'],

                    address2=data_dict['address'],
                    dayOfWeek2=WEEK_DICT[forecasts2['dayOfWeek']],
                    date2=forecasts2['date'],
                    dayWeather2=forecasts2['dayWeather'],
                    dayTemp2=forecasts2['dayTemp'],
                    dayWindDirection2=forecasts2['dayWindDirection'],
                    dayWindPower2=forecasts2['dayWindPower'],
                )

                print(return_text)
                return return_text
            else:
                print('获取天气失败:{}'.format(content_dict['msg']))
                # return None
        print('获取天气失败。')
    except Exception as exception:
        print(str(exception))
        # return None
    # return None


# get_today_weather = get_rttodayweather


if __name__ == '__main__':
    cityname = '普陀区'
    weather = get_rttodayweather(cityname)
    # print(weather)
    pass
